<?php


namespace App\Http\Model\Job;


use App\Enum\ClassLessonHistoryEnum;
use Illuminate\Support\Facades\DB;

class PfsHeadAddclasslessonModel
{

    /**
     * 2.4排课信息同步>机构端>招行:同步结果入库
     *
     * @param        $data
     * @param        $result
     * @param string $errMsg
     *
     * @return int
     */
    public static function save($data, $result, $errMsg = '')
    {
        if (!empty($result->data->success) && $result->data->success == true) {
            $errMsg = $result->message;
            $result = ClassLessonHistoryEnum::RESULT_SUCCESS;
        } else {
            $result = ClassLessonHistoryEnum::RESULT_FAILURE;
        }
        $ins = [];
        foreach ($data['list'] as $val) {
            /*查询同步记录是否已存在*/
            $exist = DB::select('SELECT `id` FROM `class_lesson_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `dt`=:dt AND `lessonindex`=:lessonindex AND `class_id`=:class_id AND `isdel`=0 ORDER BY id DESC LIMIT 1', [
                'pfs_dw_id'   => $data['pfs_dw_id'],
                'dt'          => $val->dt,
                'lessonindex' => $val->lessonindex,
                'class_id'    => $val->class_id,
            ]);
            $arr = [
                'push_time'           => date('Y-m-d H:i:s'),
                'result'              => $result,
                'result_desc'         => $errMsg,
                'pfs_dw_id'           => $data['pfs_dw_id'],
                'ledger_subject_name' => $val->ledger_subject_name,
                'ledger_subject_id'   => $val->ledger_subject_id,
                'org_class_id'        => $val->org_class_id,
                'class_id'            => $val->class_id,
                'lessonindex'         => $val->lessonindex,
                'lesssonname'         => $val->lesssonname,
                'lessondate'          => $val->lessondate,
                'classhour'           => $val->classhour,
                'dt'                  => $val->dt,
            ];
            if (empty($exist)) {
                $ins[] = $arr;
            } else {
                DB::table('class_lesson_history')->where('id', current($exist)->id)->update($arr);
            }
        }
        if (!empty($ins)) {
            DB::table('class_lesson_history')->insert($ins);
        }
        return 0;
    }

}





/**
 * PfsHeadAddclasslessonModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/7/15 14:43:00   操作:创建
 **/
